<?php


namespace App\User\Services;

use App\BaseService;
use App\User\Models\Ship;
use Illuminate\Support\Facades\DB;

class ShipService extends BaseService
{
    /**
     * User: zhangchaoqun
     * DateTime: 2021/6/15 11:38 上午
     * Describe: 获取收货地址列表
     * Version: V1.0
     * @param array $params
     */
    public function getList(array $params, int $limit)
    {
        return Ship::where('u_id', '=', $params['user_id'])
            ->orderBy('id', 'desc')
            ->paginate($limit);
    }

    /**
     * User: zhangchaoqun
     * DateTime: 2021/6/15 11:36 上午
     * Describe: 设置默认地址
     * Version: V1.0
     * @param int $id 收货地址ID
     * @param int $uId 用户ID
     * @param int $isDefault 是否默认
     * @throws \Exception
     */
    public function setDefault(array $params)
    {
        // 获取地址详情
        $data = Ship::find($params['id']);
        if (!$data) {
            throw new \Exception('收货地址不存在');
        }

        // 校验是否越权
        if ($data->u_id != $params['user_id']) {
            throw new \Exception('请勿非法操作');
        }

        try {
            // 判断是否存在其他默认地址
            if ($params['is_default'] == Ship::DEFAULT_ON) {
                $exists = Ship::where('u_id', '=', $params['user_id'])
                    ->where('is_default', '=', Ship::DEFAULT_ON)
                    ->exists();
                if ($exists) {
                    $state = Ship::where('u_id', '=', $params['user_id'])->update([
                        'is_default' => Ship::DEFAULT_OFF
                    ]);
                    if (!$state) {
                        throw new \Exception('更新默认地址失败，请联系客服');
                    }
                }
            }

            $data->is_default = $params['is_default'];
            if (!$data->save()) {
                throw new \Exception('设置失败，请联系客服人员');
            }

        } catch (\Exception $exception) {
            throw new \Exception($exception->getMessage());
        }
    }

    /**
     * User: zhangchaoqun
     * DateTime: 2021/6/15 11:09 上午
     * Describe: 添加收货地址
     * Version: V1.0
     * @param array $params
     */
    public function addData(array $params)
    {
        $date = date('Y-m-d H:i:s');
        $addData = [
            'u_id' => $params['user_id'],
            'a_id' => $params['a_id'],
            'district' => $params['district'],
            'address' => $params['address'],
            'name' => $params['name'],
            'mobile' => $params['mobile'],
            'created_at' => $date,
            'updated_at' => $date
        ];

        if (!Ship::insert($addData)) {
            throw new \Exception('添加收货地址失败，请联系客服人员');
        }
    }

    /**
     * User: zhangchaoqun
     * DateTime: 2021/6/15 11:17 上午
     * Describe: 修改收货地址
     * Version: V1.0
     * @param array $params
     */
    public function editData(array $params)
    {
        // 获取地址详情
        $data = Ship::find($params['id']);
        if (!$data) {
            throw new \Exception('收货地址不存在');
        }

        // 校验是否越权
        if ($data->u_id != $params['user_id']) {
            throw new \Exception('请勿非法操作');
        }

        $data->a_id = $params['a_id'];
        $data->name = $params['name'];
        $data->district = $params['district'];
        $data->address = $params['address'];
        $data->mobile = $params['mobile'];

        if (!$data->save()) {
            throw new \Exception('保存失败，请联系客服人员');
        }
    }

    /**
     * User: zhangchaoqun
     * DateTime: 2021/6/15 11:31 上午
     * Describe: 获取收货地址详情
     * Version: V1.0
     * @param array $params
     */
    public function getDetail(array $params)
    {
        $where = [
            'u_id' => $params['user_id']
        ];

        if (isset($params['is_default']) && $params['is_default'] == Ship::DEFAULT_ON) {
            $where['is_default'] = Ship::DEFAULT_ON;
        } else {
            $where['id'] = $params['id'];
        }

        $data = Ship::where($where)->first();
        if (!$data) {
            throw new \Exception('收货地址不存在');
        }

        return $data;
    }

    /**
     * User: zhangchaoqun
     * DateTime: 2021/6/15 1:00 下午
     * Describe: 删除收货地址
     * Version: V1.0
     * @param array $params
     * @throws \Exception
     */
    public function delData(array $params)
    {
        $data = Ship::where('id', '=', $params['id'])
            ->where('u_id', '=', $params['user_id'])
            ->first();
        if (!$data) {
            throw new \Exception('收货地址不存在');
        }

        if (!Ship::where('id', '=', $params['id'])->delete()) {
            throw new \Exception('删除失败，请联系客服人员');
        }
    }
}
